package com.ftdigital.primemovers_operators;

import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.bugtags.library.Bugtags;
import com.ftdigital.primemovers_operators.fragment.LoginFragment_;
import com.ftdigital.primemovers_operators.model.AddDetail;
import com.ftdigital.primemovers_operators.util.loading.Loading;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.umeng.update.UmengUpdateAgent;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;

@EActivity(R.layout.index_main)
public class IndexActivity extends FragmentActivity {

    static FragmentManager fragmentManager;

    static IndexActivity indexActivity;

    public static IndexActivity getInstace() {
        return indexActivity;
    }

    @AfterViews
    void afterView(){
        setTranslucentStatus();
        UmengUpdateAgent.update(this);
        UmengUpdateAgent.setUpdateOnlyWifi(false);


        indexActivity = this;
        Loading.context = this;
        fragmentManager = getSupportFragmentManager();
        getSupportFragmentManager()
                .beginTransaction().replace(R.id.index_container, new LoginFragment_()).commit();
    }




    private boolean is2CallBack = false;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (fragmentManager.getBackStackEntryCount() != 0) {
                fragmentManager.popBackStack();
            } else {
                if (!is2CallBack) {
                    is2CallBack = true;
                    Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            is2CallBack = false;
                        }
                    }, 1500);
                } else {
                    android.os.Process.killProcess(android.os.Process.myPid());
                }
            }
        }
        return true;
    }
    /**
     * @param from       当前fragment
     * @param toFragment 跳转到的fragment
     */
    public void switchFragment(Fragment from, Fragment toFragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .hide(from).replace(R.id.content_none_bottom, toFragment).addToBackStack(null).commit();
    }
    /**
     * 设置状态栏背景状态   设置主题统一
     */
    private void setTranslucentStatus()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            Window win = getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            winParams.flags |= bits;
            win.setAttributes(winParams);
        }


        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        // enable status bar tint
        tintManager.setStatusBarTintEnabled(true);
        // enable navigation bar tint
        tintManager.setNavigationBarTintEnabled(true);
        // set a custom tint color for all system bars
        tintManager.setTintColor(Color.parseColor("#09ff8b0a"));
    }

    @Override
    protected void onResume() {
        super.onResume();
        //注：回调 1
        Bugtags.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //注：回调 2
        Bugtags.onPause(this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        //注：回调 3
        Bugtags.onDispatchTouchEvent(this, event);
        return super.dispatchTouchEvent(event);
    }

}
